using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Util;
using TTDotNet.Engine.Terrain;
using TTDotNet.Engine.GUI;
using TTDotNet.Engine.GUI.Buttons;
using TTDotNet.Engine.Create.Tools;

namespace TTDotNet.Engine.GUI.Windows
{
    public class LandscapingWindow : Window
    {
        private int bSize = 48;
        private int cSize = 24;
        private int border = 2;

        public LandscapingWindow(PointF screenLocation)
        {
            int width = (7 * bSize) + (8 * border);
            int height = cSize + bSize + (3 * border);

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            int controlNum = 0;

            //Game.newGUI.CreatePanel(panelID, new Point(coord.X, coord.Z), new Size(width, height), new Background("artwork/colours/dark_spring_green.jpg"));
            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            //Close button. 
            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            "Landscaping", 14, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            RadioGroup group = new RadioGroup();
            windowControls[controlNum++] = group;

            int rbNum = 0;

            RadioButton lower = new RadioButton(1, new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/landscape_lower.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/landscape_lower_down.jpg"));
            lower.OnControl += new Control.ControlDelegate(SetToLower);
            group.Add(rbNum++, lower);

            RadioButton raise = new RadioButton(1, new RectangleF(new PointF((2 * border) + (bSize * 1), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/landscape_raise.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/landscape_raise_down.jpg"));
            raise.OnControl += new Control.ControlDelegate(SetToRaise);
            group.Add(rbNum++, raise);

            RadioButton level = new RadioButton(1, new RectangleF(new PointF((3 * border) + (bSize * 2), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.White, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/landscape_level.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/landscape_level_down.jpg"));
            group.Add(rbNum++, level);
            

            RadioButton dynamite = new RadioButton(1, new RectangleF(new PointF((4 * border) + (bSize * 3), cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/dynamite.jpg"),
                                            Game.textureCacher.LoadTexture("artwork/128/dynamite_down.jpg"));
            group.Add(rbNum++, dynamite);

            //windowControls[controlNum++] = new RadioButton(1, new RectangleF(new PointF((5 * border) + (bSize * 4), cSize + (2 * border)), new SizeF(bSize, bSize)),
            //                                "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/build_lorry_bay.jpg"),
            //                                Game.textureCacher.LoadTexture("artwork/128/build_lorry_bay_down.jpg"));

            //Game.newGUI.CreateRadioButton(ControlType.None, null, Game.newGUI.NextFreeID(), panelID, 1, new Point((5 * border) + (bSize * 4), cSize + (2 * border)),
            //                            new Size(bSize, bSize), "", 14, Color.White, "Arial", false, true, new Background("artwork/128/buy_land.jpg"),
            //                            new Background("artwork/128/buy_land_down.jpg"));

            //Game.newGUI.CreateRadioButton(ControlType.None, null, Game.newGUI.NextFreeID(), panelID, 1, new Point((6 * border) + (bSize * 5), cSize + (2 * border)),
            //                            new Size(bSize, bSize), "", 14, Color.White, "Arial", false, true, new Background("artwork/128/trees.jpg"),
            //                            new Background("artwork/128/trees_down.jpg"));

            //Game.newGUI.CreateRadioButton(ControlType.None, null, Game.newGUI.NextFreeID(), panelID, 1, new Point((7 * border) + (bSize * 6), cSize + (2 * border)),
            //                            new Size(bSize, bSize), "", 14, Color.White, "Arial", false, true, new Background("artwork/128/sign.jpg"),
            //                            new Background("artwork/128/sign_down.jpg"));

        }

        private void SetToRaise(object data, object tag)
        {
            if ((ControlStateChange)data == ControlStateChange.Selected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.Landscaping, true);
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.Raise;
            }
            else if ((ControlStateChange)data == ControlStateChange.Deselected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.Landscaping, false);
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.None;
            }
        }

        private void SetToLower(object data, object tag)
        {
            if ((ControlStateChange)data == ControlStateChange.Selected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.Landscaping, true);
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.Lower;
            }
            else if ((ControlStateChange)data == ControlStateChange.Deselected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.Landscaping, false);
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.None;
            }
        }

        private void SetToLevel(int controlID, object data)
        {
            if ((ControlStateChange)data == ControlStateChange.Selected)
            {
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.Level;
            }
            else if ((ControlStateChange)data == ControlStateChange.Deselected)
            {
                (Game.toolbox.GetTool(ToolType.Landscaping) as LandscapingTool).ActiveTool = LandscapingType.None;
            }
        }

        public override void Close()
        {
            Game.toolbox.ChangeToolStatus(ToolType.Landscaping, false);

            base.Close();
        }


    }
}
